package jp.sourceforge.akjrcp.ui;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import jp.sourceforge.akjrcp.flvtools.basic.SimpleView;
import jp.sourceforge.akjrcp.flvtools.search.FirefoxUtil;
import jp.sourceforge.akjrcp.flvtools.search.YoutubeSearchKey;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;



public class DirectoryList extends TableViewer{
	protected ArrayList fileList=new ArrayList();
	public DirectoryList(Composite parent,int style){
		super(parent,style);
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
	}
	public DirectoryList(Composite parent) {
		super(parent);
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
	}
	
	protected FilenameFilter filenameFilter;
	protected File baseDirectory;
	

	
	protected static  Image fileImage =new Image(Display.getCurrent(), DirectoryList.class.getClassLoader()
            .getResourceAsStream(
            "jp/sourceforge/akjrcp/resource/file.png"));;
            protected static Image folderImage =new Image(Display.getCurrent(), DirectoryList.class.getClassLoader()
            .getResourceAsStream(
            "jp/sourceforge/akjrcp/resource/folder.png"));;
	
	
            class ViewContentProvider implements IStructuredContentProvider {
        		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        			
        		}
        		public void dispose() {
        		}
        		public Object[] getElements(Object parent) {
        			return fileList.toArray();
        		}
        	}
            
        	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        		public String getColumnText(Object obj, int index) {
        			return ((File)obj).getName();
        		}
        		public Image getColumnImage(Object obj, int index) {
        			return getImage(obj);
        		}
        		public Image getImage(Object obj) {
        			File file=(File)obj;
        			if(file.isDirectory()){
        				return folderImage;
        			}else{
        				return fileImage;
        			}
        		}
        	}
	
	
	
	public void updateDirectory(){
		fileList.removeAll(fileList);
		
		String list[]=null;
		if(filenameFilter!=null){
			list=baseDirectory.list(filenameFilter);
		}else{
			list=baseDirectory.list();
		}
		
		for (int i = 0; i < list.length; i++) {
			addFile(new File(baseDirectory,list[i]));
		}
		//add
		/*
		 String[] columns=getColumnTexts();
		for (int i = 0; i < list.length; i++) {
			if(!isExist(columns,list[i])){
			
				addFile(new File(baseDirectory,list[i]));
			}
		}*/
		/*
		//delete..
		for (int i = 0; i < columns.length; i++) {
			if(!isExist(list,columns[i])){
				removeFile(columns[i]);
			}
		}*/
	}
	public void openDirectory(String path){
		setBaseDirectory(new File(path));
		updateDirectory();
		this.refresh();
	}
	protected void removeFile(String name) {
		File file=new File(baseDirectory,name);
		fileList.remove(file);
	}

	protected boolean isExist(String base[],String name){
		for (int i = 0; i < base.length; i++) {
			if(base[i].equals(name)){
				return true;
			}
		}
		return false;
	}
	protected void addFile(File file){
		SimpleView.log.info("add:"+file.getAbsolutePath());
		fileList.add(file);
	}
	

	public File getBaseDirectory() {
		return baseDirectory;
	}
	public void setBaseDirectory(File baseDirectory) {
		this.baseDirectory = baseDirectory;
	}
	public FilenameFilter getFilenameFilter() {
		return filenameFilter;
	}
	
	public void setFilenameFilter(FilenameFilter filenameFilter) {
		this.filenameFilter = filenameFilter;
	}
	public File[] getFileList() {
		return (File[])fileList.toArray(new File[0]);
	}
	public File[] getSelectionFiles() {
		ISelection selection=this.getSelection();
		return (File[]) ((IStructuredSelection)selection).toList().toArray(new File[0]);
	}
	
	
}
